package org.gap.wizards.items;

import java.util.ArrayList;
import java.util.List;

import org.gap.wizards.utils.Persistence;

public class Inventory {
	private List<Item> equippedItems;
	private List<Item> allItems;
	private Money totalMoney;

	public Inventory() {
		equippedItems = new ArrayList<Item>();
		allItems = new ArrayList<Item>();
		totalMoney = new Money(0);
	}

	public void addItem(Item item) {
		allItems.add(item);
	}

	public void removeItem(Item item) {
		boolean success = allItems.remove(item);
		if (!success) {
			allItems.contains(item);
			throw new RuntimeException("Failed to remove item");
		}
	}

	public int getTotalMoney() {
		return totalMoney.getAmount();
	}

	public List<Item> allItems() {
		List<Item> items = new ArrayList<Item>();
		items.addAll(allItems);
		return items;
	}

	public void persist(Persistence persister) {
		totalMoney.persist(persister);
	}

	public void load(Persistence persister) {
		totalMoney.load(persister);
	}

	@Override
	public boolean equals(Object obj) {
		if (obj == null) {
			return false;
		}
		if (obj.getClass().equals(Inventory.class)) {
			Inventory other = (Inventory) obj;
			return totalMoney.equals(other.totalMoney);
		}
		return false;
	}

	public List<Item> itemsOfType(Class<? extends Item> klass) {
		List<Item> result = new ArrayList<Item>();
		for (Item each : allItems) {
			if (klass.isInstance(each)) {
				result.add(each);
			}
		}
		return result;
	}

	public List<Item> itemsWithFeature(Feature feature) {
		List<Item> result = new ArrayList<Item>();
		for (Item each : allItems) {
			if (feature.hasFeature(each)) {
				result.add(each);
			}
		}
		return result;
	}
	
	public interface Feature {

		boolean hasFeature(Item each);

	}

	public void equipItem(Item each) {
		equippedItems.add(each);
	}

	public List<Item> getEquipedItems() {
		return equippedItems;
	}

	public boolean isEmpty() {
		return allItems().isEmpty();
	}
}
